﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;

namespace Orange.Mvc4App.Services {

    public interface ICustomerService : IBaseService<Customer> {
        Customer GetNext();
    }

    public class CustomerService : BaseService, ICustomerService {
        public IList<Customer> GetList(Func<Customer, bool> filters) {
            if (filters != null)
                return db.Customers.Where(filters).ToList();
            else
                return db.Customers.ToList();
        }

        public void Add(Customer model) {
            db.Customers.Add(model);
        }

        public void Update(Customer model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(Customer model) {
            db.Customers.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public Customer GetNext() {
            int id = 1;
            if (db.Customers.Count() > 0)
                id = db.Customers.Max(c => c.CustomerId) + 1;
            return new Customer { CustomerId = id };
        }


        public Customer Find(params object[] keys) {
            return db.Customers.Find(keys);
        }
    }
}